#!/bin/bash

# Color
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# 1. Define OLM address
OLM_ADDRESS="https://gitee.com/uccps-samples/uccps-operators/raw/master/olm/operator-lifecycle-manager/manifests/"  # Specify the address of OLM

# 2. Uninstall UCCPS catalog
echo -e "${BLUE}Uninstalling UCCPS catalog...${NC}"
kubectl delete -f $OLM_ADDRESS/catalog.yaml -n uccps-operators

# 3. Uninstall OLM manifests
echo -e "${BLUE}Uninstalling OLM...${NC}"
kubectl delete -f $OLM_ADDRESS/olm.yaml
kubectl delete -f $OLM_ADDRESS/crd.yaml

# 4. Verify OLM uninstallation
echo -e "${YELLOW}Verifying OLM uninstallation...${NC}"
olm_pods=$(kubectl get pods -n olm --no-headers -o custom-columns=":metadata.name")

if [ -z "$olm_pods" ]; then
  echo -e "${GREEN}OLM components have been successfully uninstalled.${NC}"
else
  echo -e "${RED}Some OLM components are still running:${NC}"
  echo "$olm_pods"
  echo -e "${RED}Please check the status of these components manually.${NC}"
  exit 1
fi

# 5. Check if UCCPS catalog pods are terminated
uccps_pods=$(kubectl get pods -n uccps-operators --no-headers -o custom-columns=":metadata.name")

if [ -z "$uccps_pods" ]; then
  echo -e "${GREEN}UCCPS catalog has been successfully uninstalled.${NC}"
else
  echo -e "${RED}Some UCCPS catalog components are still running:${NC}"
  echo "$uccps_pods"
  echo -e "${RED}Please check the status of these components manually.${NC}"
  exit 1
fi

echo -e "${GREEN}OLM and UCCPS catalog have been completely uninstalled.${NC}"